<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_kode_keuangan extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_desa';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$nama_desa = isset($_POST['nama_desa']) ? strval($_POST['nama_desa']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*');
			$this->db->from('desa');
			
			
			if($nama_desa != ''){
				$this->db->like('nama_desa', $nama_desa, 'both');
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, b.nama_prodi, d.kd_dosen, d.nama_dosen, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.kd_prodi, b.kd_fakultas, c.nama_fakultas, a.tempat_lahir, a.tgl_lahir, a.kd_jk, a.kd_kabup, e.nama_kabup, e.kd_prov, f.nama_prov, a.kd_program, g.Nama as nama_program ');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		$this->db->join('ref_kabupaten e', 'e.kd_kabup = a.kd_kabup','left');
		$this->db->join('ref_provinsi f', 'f.kd_prov = e.kd_prov','left');
		$this->db->join('ref_program g', 'g.kd_program = a.kd_program','left');
		
		$this->db->where('a.kd_mahasiswa', $kode);
		return $this->db->get();
	}
	
	function getdataByNIM($nim){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->join('ref_program d', 'd.kd_program = a.kd_program');
		$this->db->where('a.nim', $nim);
		
		return $this->db->get();
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('desa');
		
		return $this->db->get()->num_rows();
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_desa',$kode);
		
		$this->db->select('*');
		$this->db->from('desa');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kode_desa', $data['kode_desa']);
		$this->db->set('nama_desa', $data['nama_desa']);
		$result = $this->db->insert('desa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
}

?>